{
	let $time = $(".sub-title>span");

	let dateTimeFormat = new Intl.DateTimeFormat('zh-CN', {
		year: 'numeric',
		month: 'long',
		day: 'numeric',
		hour: 'numeric',
		minute: "numeric",
		second: "numeric",
		hour12: false
	});

	function setDateTime() {
		$time.html(dateTimeFormat.format(new Date()));
		setInterval(() => {
			$time.html(dateTimeFormat.format(new Date()));
		}, 1000);
	}
	setDateTime();

	let $confirm = $("#dataCount .number[type=confirm]");
	let $heal = $("#dataCount .number[type=heal]");
	let $dead = $("#dataCount .number[type=dead]");
	let $nowConfirm = $("#dataCount .number[type=nowConfirm]");
	let $noInfect = $("#dataCount .number[type=noInfect]");
	let $importedCase = $("#dataCount .number[type=importedCase]");

	$.fn.numberRock=function(options){
		let defaults={
			lastNumber:100,
			duration:5000,
			easing:'swing'
		};
		let opts=$.extend({}, defaults, options);
		$(this).animate({
			num : "numberRock",
		},{
			duration : opts.duration,
			easing : opts.easing,
			complete : function(){},
			step : function(a,b){
				$(this).html(parseInt(b.pos * opts.lastNumber));
			}
		});
	}

	// echarts 初始化
	let loadingStyle = {
		text: '数据载入中',
		color: '#65c4f1',
		textColor: '#65c4f1',
		maskColor: 'rgba(255, 255, 255, 0)'
	}

	let xyDefaultOption = {
		tooltip: {
			trigger: "axis",
			axisPointer: {
				type: "line"
			}
		},
		legend: {},
		xAxis: {
			type: 'category'
		},
		yAxis: {
			axisLabel: {
				formatter: function (value, index) {
					// console.log(value);
					let num = Number.parseInt(value / 1000);
					return num > 0 ? num + "k" : value;
				}
			}
		},
	}

	// 为echarts封装异步请求错误后，消息提示和关闭loading功能的方法
	function initEchartsBox(el, theme = "dark") {
		let temp = echarts.init($(el)[0], theme);
		temp._$msg = $(el).siblings(".msg");
		temp.hideErrorLoading = function (msg = "数据载入失败") {
			this.hideLoading();
			this._$msg.html(msg);
			this._$msg.css("display", "block");
		}
		return temp;
	}

	let cov19CumulativeChart = initEchartsBox("#cov19-cumulative");
	let cov19New = initEchartsBox("#cov19-new");
	let cov19ProvincialStatistics = initEchartsBox("#cov19-provincialStatistics");
	let cov19OutsideTheInput = initEchartsBox("#cov19-outsideTheInput");
	let cov19Map = initEchartsBox("#cov19-map");

	cov19CumulativeChart.showLoading(loadingStyle);
	cov19New.showLoading(loadingStyle);
	cov19ProvincialStatistics.showLoading(loadingStyle);
	cov19OutsideTheInput.showLoading(loadingStyle);
	// cov19Map.showLoading(loadingStyle);

	cov19OutsideTheInput.setOption({
		tooltip: {
			trigger: 'item',
			formatter: '{a} <br/>{b} : {c} ({d}%)'
		},
		legend: {
			orient: 'vertical',
			left: 'left',
			textStyle: {
				color: 'white',
			},
		},
		series: [{
			type: 'pie',
			radius: '70%',
			center: ['50%', '50%'],
			data: [],
			emphasis: {
				itemStyle: {
					shadowBlur: 10,
					shadowOffsetX: 0,
					shadowColor: 'rgba(0, 0, 0, 0.5)'
				}
			}
		}]
	});
	cov19Map.setOption({
		tooltip: {
			trigger: 'item'
		},
		visualMap: { // 左侧小导航图标
			show: true,
			x: 'left',
			y: 'bottom',
			textStyle: {
				fontSize: 8,
				color: 'white',
			},
			splitList: [{
				start: 1,
				end: 9
			},
			{
				start: 10,
				end: 99
			},
			{
				start: 100,
				end: 999
			},
			{
				start: 1000,
				end: 9999
			},
			{
				start: 10000
			}
			],
			color: ['#8A3310', '#C64918', '#E55B25', '#F2AD92', '#F9DCD1']
		},
		series: [
			{
				name: '累计确诊人数',
				type: 'map',
				mapType: 'china',
				roam: false, // 禁用拖动和缩放
				itemStyle: { // 图形样式
					normal: {
						borderWidth: .5, //区域边框宽度
						borderColor: '#69414b', //区域边框颜色
						areaColor: '#ffefd5', //区域颜色
					},
					emphasis: { // 鼠标滑过地图高亮的相关设置
						borderWidth: .5,
						borderColor: '#69414b',
						areaColor: '#e83e8c',
					}
				},
				label: { // 图形上的文本标签
					show: true,
					color: "#343a40"
				},
				data: []
			}
		]
	})

	window.onresize = function () {
		cov19CumulativeChart.resize();
		cov19New.resize();
		cov19ProvincialStatistics.resize();
		cov19OutsideTheInput.resize();
		cov19Map.resize();
	}
	// 异步数据加载
	function loadCov19Data(name = "disease_other") {
		return new Promise(function (resolve, reject) {
			$.ajax({
				url: "https://view.inews.qq.com/g2/getOnsInfo",
				method: "get",
				data: { name },
				dataType: "jsonp",
				success: resolve,
				error: reject
			});
		})
	}

	function initCumulativeAndNew() {
		loadCov19Data("disease_other")
			.then(({ data }) => {
				let { chinaDayList, chinaDayAddList } = JSON.parse(data);
				let defaultOpt = {
					dataset: {
						dimensions: ['date', 'confirm', 'heal', 'dead'],
						source: []
					},
					series: [
						{ name: "确诊", type: 'line' },
						{ name: "治愈", type: 'line' },
						{ name: "死亡", type: 'line' }
					]
				}
				defaultOpt.dataset.source = chinaDayList;
				cov19CumulativeChart.setOption(Object.assign(xyDefaultOption, defaultOpt));
				defaultOpt.dataset.source = chinaDayAddList;
				cov19New.setOption(Object.assign(xyDefaultOption, defaultOpt));
			})
			.then(() => {
				cov19CumulativeChart.hideLoading();
				cov19New.hideLoading();
			})
			.catch(() => {
				cov19CumulativeChart.hideErrorLoading();
				cov19New.hideErrorLoading();
			});
	}
	function initOtherData() {
		loadCov19Data("disease_h5")
			.then(({ data }) => {
				let {chinaTotal, areaTree: [provincial] } = JSON.parse(data);
				let outInput = [];
				let dataMap = [];
				provincial.children.forEach((c) => {
					// console.log(c);
					dataMap.push({
						name: c.name,
						value: c.total.confirm,
						heal:c.total.heal,
						dead:c.total.dead
					});
					if (c.children[0].name == '境外输入') {
						outInput.push({ name: c.name, value: c.children[0].total.confirm })
					}
				})
				cov19Map.setOption({
					series: [{ data: dataMap }]
				});

				dataMap.sort((a, b) => b.value - a.value);
				let defaultOpt = {
					dataset: {
						dimensions: ['name', 'value', 'heal', 'dead'],
						source: dataMap.slice(0,10)
					},
					series: [
						{ name: "确诊", type: 'bar' },
						{ name: "治愈", type: 'bar' },
						{ name: "死亡", type: 'bar' }
					]
				}
				cov19ProvincialStatistics.setOption(Object.assign(xyDefaultOption, defaultOpt));

				outInput.sort((a, b) => b.value - a.value);
				outInput.length = 5;
				cov19OutsideTheInput.setOption({
					series: [{ data: outInput }]
				});

				$confirm.numberRock({
					lastNumber:chinaTotal.confirm
				});
				$heal.numberRock({
					lastNumber:chinaTotal.heal
				});
				$dead.numberRock({
					lastNumber:chinaTotal.dead
				});
				$nowConfirm.numberRock({
					lastNumber:chinaTotal.nowConfirm
				});
				$noInfect.numberRock({
					lastNumber:chinaTotal.noInfect
				});
				$importedCase.numberRock({
					lastNumber:chinaTotal.importedCase
				});


			})
			.then(() => {
				cov19ProvincialStatistics.hideLoading();
				cov19OutsideTheInput.hideLoading();
			})
			.catch(() => {
				cov19ProvincialStatistics.hideErrorLoading();
				cov19OutsideTheInput.hideErrorLoading();
			});
	}
	$(function () {
		initCumulativeAndNew();
		initOtherData();
	});
}